/*
 ============================================================================
 Name		: ListenVoaAppUi.cpp
 Author	  : JohnLZeng
 Copyright   : Your copyright notice
 Description : CListenVoaAppUi implementation
 ============================================================================
 */

// INCLUDE FILES
#include <avkon.hrh>
#include <aknmessagequerydialog.h>
#include <aknnotewrappers.h>
#include <stringloader.h>
#include <f32file.h>
#include <s32file.h>

#include <ListenVoa_0xE1EDF199.rsg>

#include "ListenVoa.hrh"
#include "ListenVoa.pan"
#include "ListenVoaApplication.h"
#include "ListenVoaAppUi.h"
#include "ListenVoaAppView.h"

#include "Utils.h"
#include "MacroUtil.h"
#include "QueryDlgUtil.h"

#include "HelpView.h"
#include "LogoView.h"
#include "DocDetailView.h"
#include "RSSBriefView.h"
#include "DownloadManageView.h"
#include "NormalSettingView.h"

#include "QQReaderModule.h"
#include "cx_moduleinterface.h"
#include "QQReaderMessageId.h"
#include "QQReaderModuleId.h"
#include "QTextSetting.h"
#include "QInstance.h"

#include "EngineManager.h"
// ============================ MEMBER FUNCTIONS ===============================


// -----------------------------------------------------------------------------
// CListenVoaAppUi::ConstructL()
// Symbian 2nd phase constructor can leave.
// -----------------------------------------------------------------------------
//
void CListenVoaAppUi::ConstructL()
	{
	// Initialise app UI with standard value.
	BaseConstructL(CAknAppUi::EAknEnableSkin);

	InitRem();
	
	iTextSetting = new(ELeave)CQTextSetting;
	iTextSetting->Init();
	CQInstance::AddWithUid(iTextSetting,CQInstance::ETextSettingInstance);

	iReaderModule = CQQReaderModule::NewL();	
	cx_sendmessage(EMessage_BookCity_Browse_Start,0,0,EModule_Controller_BookCity,-1);
	
	iDataModel = CSHModel::NewL();
	iUIMgr = CUIManager::NewL();
	iEngineManager = CEngineManager::NewL();
	iEngineManager->Init();
	

	// Create view object
//	iAppView = CListenVoaAppView::NewL();
//	AddViewL(iAppView);

	iHelpView = CHelpView::NewL();
	AddViewL(iHelpView);
	iLogoView = CLogoView::NewL();
	AddViewL(iLogoView);
	iDocDetail = CDocDetailView::NewL();
	AddViewL(iDocDetail);
	iRssBrief = CRSSBriefView::NewL();
	AddViewL(iRssBrief);
	iDownloadManagerView = CDownloadManageView::NewL();
	AddViewL(iDownloadManagerView);
	iSettingView = CNormalSettingView::NewL();
	AddViewL(iSettingView);

//	SetDefaultViewL(*iLogoView);
	SetDefaultViewL(*iRssBrief);

	//add your code here...

	}
// -----------------------------------------------------------------------------
// CListenVoaAppUi::CListenVoaAppUi()
// C++ default constructor can NOT contain any code, that might leave.
// -----------------------------------------------------------------------------
//
CListenVoaAppUi::CListenVoaAppUi()
	{
	// No implementation required
	iAppView = NULL;
	}

// -----------------------------------------------------------------------------
// CListenVoaAppUi::~CListenVoaAppUi()
// Destructor.
// -----------------------------------------------------------------------------
//
CListenVoaAppUi::~CListenVoaAppUi()
	{
	//add your code here...
	SAFE_DELETE	(iDataModel)
	SAFE_DELETE(iUIMgr)
	if (iEngineManager)
		{
		iEngineManager->Release();
		delete iEngineManager;
		iEngineManager = NULL;
		}
	
	SAFE_DELETE(iReaderModule);
	SAFE_DELETE(iTextSetting);
	CQInstance::ResetAndDestroy();
	CloseSTDLIB();
	
	ReleaseRem();
	}

// -----------------------------------------------------------------------------
// CListenVoaAppUi::HandleCommandL()
// Takes care of command handling.
// -----------------------------------------------------------------------------
//
void CListenVoaAppUi::HandleCommandL(TInt aCommand)
	{
	switch (aCommand)
		{
		case EEikCmdExit:
		case EAknSoftkeyExit:
			Exit();
			break;
		case ECommandHelp:
			ActivateLocalViewL(TUid::Uid(ESHHelpViewId));
			break;
		case ECommandAbout:
			About();
			break;
		default:
			Panic(EListenVoaUi);
			break;
		}
	}
// -----------------------------------------------------------------------------
//  Called by the framework when the application status pane
//  size is changed.  Passes the new client rectangle to the
//  AppView
// -----------------------------------------------------------------------------
//
void CListenVoaAppUi::HandleStatusPaneSizeChange()
	{
	if (iAppView != NULL)
		iAppView->HandleStatusPaneSizeChange();
	}

void CListenVoaAppUi::SHError(const TSHErrInfo& aInfo,
		const TSHErrState& aState)
	{
	switch (aInfo)
		{
		default:
			break;
		}

	switch (aState)
		{
		case ESHErrSerious:
			Exit();
			break;
		default:
			break;
		}
	}

void CListenVoaAppUi::About()
	{
	TFileName filename;
	GetAppPath(filename);
	HBufC* textResource = StringLoader::LoadLC(R_ABOUT_FILE);
	filename.Append(textResource->Des());
	CleanupStack::PopAndDestroy(textResource);

	RFile file;
	TInt nErr = file.Open(CEikonEnv::Static()->FsSession(), filename, EFileRead
			| EFileShareAny);
	if (nErr != KErrNone)
		return;

	TFileText fileText;
	fileText.Set(file);
	TBuf<128> linePtr;
	HBufC* iText = NULL;

	while (fileText.Read(linePtr) == KErrNone)
		{
		if (iText != NULL)
			{
			iText = iText->ReAllocL(iText->Length() + linePtr.Length() + 2);
			iText->Des().Append(linePtr);
			}
		else
			{
			iText = HBufC::NewL(linePtr.Length() + 2);
			iText->Des().Append(linePtr);
			}
		iText->Des().Append(CEditableText::ELineBreak);
		}
	file.Close();

	ShowModalAboutDlgL(R_ABOUT_DIALOG_TITLE, iText->Des());

	delete iText;
	}

void CListenVoaAppUi::InitRem()
	{
	  iInterfaceSelector = CRemConInterfaceSelector::NewL();     
	  iCoreTarget = CRemConCoreApiTarget::NewL(*iInterfaceSelector, *this);     
	  iInterfaceSelector->OpenTargetL();
	}
void CListenVoaAppUi::ReleaseRem()
	{
//	SAFE_DELETE(iCoreTarget)
	SAFE_DELETE(iInterfaceSelector)
	}

void CListenVoaAppUi::MrccatoCommand(
		TRemConCoreApiOperationId aOperationId,
		TRemConCoreApiButtonAction aButtonAct)
	{
	TRequestStatus status;
	switch (aOperationId)
		{
		case ERemConCoreApiVolumeUp:
			{
			switch (aButtonAct)
				{
				case ERemConCoreApiButtonPress:
					// Play/Pause button pressed                     
					break;
				case ERemConCoreApiButtonRelease:
					// Play/Pause button released                     
					break;
				case ERemConCoreApiButtonClick:
					{
					TVwsViewId view;
					GetActiveViewId(view);
					if (view.iViewUid == TUid::Uid(EListenVoaDocDetailViewId))
						iDocDetail->HandleCommandL(ECommandIncreaseVolume);
					// Play/Pause button clicked    
					}
					break;
				default:
					// Play/Pause unknown action                     
					break;
				}

			iCoreTarget->VolumeUpResponse(status, KErrNone);
			User::WaitForRequest(status);
			break;
			}
		case ERemConCoreApiVolumeDown:
			{
			switch (aButtonAct)
				{
				case ERemConCoreApiButtonPress:
					// Play/Pause button pressed                     
					break;
				case ERemConCoreApiButtonRelease:
					// Play/Pause button released                     
					break;
				case ERemConCoreApiButtonClick:
					{
					TVwsViewId view;
					GetActiveViewId(view);
					if (view.iViewUid == TUid::Uid(EListenVoaDocDetailViewId))
						iDocDetail->HandleCommandL(ECommandDecreaseVolume);
					}
					break;
				default:
					// Play/Pause unknown action                     
					break;
				}

			iCoreTarget->VolumeDownResponse(status, KErrNone);
			User::WaitForRequest(status);
			break;
			}
		}
	}
// End of File
